package com.bRoom.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

public class DateTimeUtil {

    private  static  final  String DATE_TYPE="yyyy-MM-dd HH:mm:ss";

    public  static  String DatetoStr(Date date){
        if (date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime=new DateTime();
        return dateTime.toString(DATE_TYPE);
    }

    public  static  Date StringToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter= DateTimeFormat.forPattern(DATE_TYPE);
        DateTime dateTime=dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static void main(String[] args) {
            System.out.printf(DateTimeUtil.DatetoStr(new Date()));
            System.out.println(DateTimeUtil.StringToDate("2010-10-2 11:11:11"));

        DateTime begin = new DateTime("2012-02-01");
        DateTime end = new DateTime("2012-03-01");

        Period p = new Period(begin, end, PeriodType.days());
        int days = p.getDays();
        System.out.println(days);

    }
}
